function getData(cfg) {
  const headers = new Headers({
    'Accept': 'application/json', // 通过头指定，获取的数据类型是JSON
    // 'Token': 'jl3hjdmg_4kbtq3rdhd7n5b7bd62392b35'
  })
  const config = {
    method: cfg.method,
    headers,
    mode: 'cors', //跨域
    // credentials: 'include',  // 携带cookie
  }

  let path = 'http://localhost:5000/api'

  cfg.url = path + cfg.url

  if (cfg.method === 'POST') {
    config.body = JSON.stringify(cfg.data)
  }

  const request = fetch(cfg.url, config).then(res => res.json())
  
  return request
}

export default {
  get(url) {
    return getData({
      url,
      method: 'GET'
    })
  },
  post(url, data = {}) {
    return getData({
      url,
      method: 'POST',
      data
    })
  }
}
